<?php

/**
 * TaxPanelController is the default controller for this application
 */
class TaxPanelController extends BrowseControllerBase {
	/*
	 * UserTaxDataService Object
	 */
	private $userTaxDataService;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		$this->requireUserLogin ();
		
		//Check if Blogging Module is enabled
		if ($this->currentPackageOption) {
			$module = MODULE_ECOMMERCE;
			$isEnabled = $this->currentPackageOption->isEnabled ( $module );
			if ($isEnabled == false) {
				$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Index/errorAuthor';
				CommonUtils::redirect ( $url );
				return - 1;
			}
		}
		
		// Init all DataService
		$this->userTaxDataService = new UserTaxDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	
	public function indexAction() {
		$authentication = new Authentication ();
		
		$arrElement = array ();
		$arrElement ['ownerId'] = $authentication->getCurrentUserId ();
		
		$itemList = $this->userTaxDataService->getItems ( $arrElement );
		
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'itemCount', count ( $itemList ) );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/list_tax.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function createAction() {
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		// Get list category of tax
		$authentication = new Authentication ();
		
		$isError = false;
		$errorIndex = 0;
		$error = array ();
		
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->isDemoMode == false) {
			
			// head title
			$description = $this->getPost ( 'description', "" );
			
			// Get description field
			$name = $this->getPost ( 'name', "" );
			
			// keywords
			$rate = $this->getPost ( 'rate', "" );
			
			if (! is_numeric ( $rate )) {
				$error [$errorIndex] = 'rate is not number';
				$isError = true;
				$errorIndex ++;
			}
			$rate = intval ( $rate );
			if ($rate > 100 && $rate >= 0) {
				$error [$errorIndex] = 'rate must larger or equal 0 and smaller 100';
				$isError = true;
				$errorIndex ++;
			}
			
			// check
			if (! empty ( $name ) && $isError == false) {
				try {
					// Save data into database
					$taxEntity = $this->userTaxDataService->beforeCreate ();
					
					$taxEntity->name = $name;
					$taxEntity->description = $description;
					$taxEntity->rate = $rate;
					$taxEntity->ownerId = $authentication->getCurrentUserId ();
					$this->userTaxDataService->create ( $taxEntity );
					
					// Redirect to edit page
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/TaxPanel/index';
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					
					$isError = true;
					// Ignore to process this case. Only show a message
					$error [$errorIndex] = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					$errorIndex ++;
				}
			} else {
				$isError = true;
				
				$error [$errorIndex] = 'Please fill in all required fields';
				$errorIndex ++;
			}
		}
		
		$this->smarty->assign ( 'error', $error );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/create_tax.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function editAction() {
		$authentication = new Authentication ();
		
		// Get id of page
		$id = $this->getPost ( 'id' );
		if (empty ( $id )) {
			$id = $this->getQuery ( 'id' );
		}
		// If the id of page does not exist, go on to view list page
		if (empty ( $id ) || ! is_numeric ( $id )) {
			$this->indexAction ();
			return false;
		}
		
		$id = intval ( $id );
		
		// Get data
		$taxEntity = $this->userTaxDataService->getById ( $id );
		
		// check data
		if (empty ( $taxEntity )) {
			$this->indexAction ();
			return false;
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		$isError = false;
		$errorIndex = 0;
		$error = array ();
		
		// submit
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->isDemoMode == false) {
			// head title
			$description = $this->getPost ( 'description', "" );
			
			// Get description field
			$name = $this->getPost ( 'name', "" );
			
			// keywords
			$rate = $this->getPost ( 'rate', "" );
			if (! is_numeric ( $rate )) {
				$error [$errorIndex] = 'rate is not number';
				$isError = true;
				$errorIndex ++;
			}
			$rate = intval ( $rate );
			if ($rate > 100 && $rate >= 0) {
				$error [$errorIndex] = 'rate must larger or equal 0 and smaller 100';
				$isError = true;
				$errorIndex ++;
			}
			
			$taxEntity->name = $name;
			$taxEntity->description = $description;
			$taxEntity->rate = $rate;
			
			// check
			if (! empty ( $name ) && $isError == false) {
				try {
					$res = $this->userTaxDataService->update ( $taxEntity );
				} catch ( Exception $ex ) {
					
					// Ignore to process this case. Only show a message
					$error [$errorIndex] = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					
					$isError = true;
					$errorIndex ++;
				}
			} else {
				$isError = true;
				
				$error [$errorIndex] = 'Please fill in all required fields';
				$errorIndex ++;
			}
		}
		
		$this->smarty->assign ( 'error', $error );
		$this->smarty->assign ( 'id', $taxEntity->id );
		$this->smarty->assign ( 'name', $taxEntity->name );
		$this->smarty->assign ( 'description', $taxEntity->description );
		$this->smarty->assign ( 'rate', $taxEntity->rate );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/edit_tax.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function delAction() {
		// Get Id of page
		$id = $this->getQuery ( 'id', 0 );
		
		if (! empty ( $id ) && is_numeric ( $id ) && $this->isDemoMode == false) {
			
			$id = intval ( $id );
			// Delete page
			$res = $this->userTaxDataService->del ( $id );
		}
		// Redirect to edit page
		$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/TaxPanel/index';
		CommonUtils::redirect ( $url );
		return;
	}
}
